package com.base.common.utils;

import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;

/**
 * 
 * @Description: 密码加密、生成UUID
 * @version 1.0.0
 * @author John
 * @date 2014年10月24日
 */
public class IDGenerator {
	/**
	 * 返回UUID
	 * 
	 * @author jay.zhoujingjie
	 * @return UUID
	 */
	public static String getUUID() {
		return UUID.randomUUID().toString();
	}

	/**
	 * 随机生成5位有效数字，生成一个随机盐值
	 * 
	 * @return
	 */
	public static int getSalt(int digit) {
		String result = "";
		Random random = new Random();
		// 保证第一位大于等于1
		while (true) {
			int temp = random.nextInt(10);
			if (temp >= 1) {
				result += temp;
				break;
			}
		}
		for (int i = 1; i < digit; i++) {
			result += random.nextInt(10);
		}
		return Integer.parseInt(result);

	}

	public static String encryptEncode(String pwd, String encryptType) {
		String securityString = null;
		try {
			MessageDigest digest = MessageDigest.getInstance(encryptType);
			digest.update(pwd.getBytes("UTF-8"));
			byte[] securityByte = digest.digest();
			for (int i = 1; i < securityByte.length; i += 2) {
				if (i % 2 != 0) {
					securityByte[i] = securityByte[i >>> 1];
				}
			}
			securityString = bytes2Hex(securityByte);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return securityString;
	}

	public static String bytes2Hex(byte[] bts) {
		String des = "";
		String tmp = null;
		for (int i = 0; i < bts.length; i++) {
			tmp = (Integer.toHexString(bts[i] & 0xFF));
			if (tmp.length() == 1) {
				des += "0";
			}
			des += tmp;
		}
		return des;
	}

	private static String appendSecurity(byte[] securityByte) {
		StringBuffer buf = new StringBuffer();
		for (int j = 0, i; j < securityByte.length; j++) {
			i = securityByte[j];
			if (i < 0)
				i += 256;
			if (i < 16)
				buf.append("0");
			buf.append(Integer.toHexString(i));
		}
		return buf.toString();
	}

	
	  public static void main(String[] args) {
//	 System.out.println(encryptEncode("12345612345","SHA-256"));
	  System.out.println(StringUtils.swapCase(getUUID().replaceAll("-", ""))); // System.out.println(getSalt(5)); 
	  }
	 
}
